package com.dongge.service;

import com.dongge.dao.RoleDao;
import com.dongge.dao.UserDao;
import com.dongge.entity.Authorization;
import com.dongge.entity.Role;
import com.dongge.entity.User;
import com.dongge.exception.RegisterException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2016/7/15.
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao;



    public boolean login(String username,String password){
        System.out.println(username+":"+password);
        User user = userDao.getUserByUsername(username);
        if(user != null && user.getPassword().equals(password)){
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            session.setAttribute("user",user);//采用shiro session来设置
            return true;
        }
        return  false;
    }

    public boolean register(String username,String password){
        try{
            userDao.register(username,password);
        }catch (RegisterException r){
            return false;
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public User getUserById(long id){
        return userDao.get(id);
    }

    public List<User> getUsers() {
        /*List<User> users = userDao.getUsers();
        Set<User> userSet = new HashSet<User>(users);
        return userSet;*/
        return userDao.getUsers();
    }

    public int delete(long id) {
        return userDao.delete(id);
    }

    public long save(User user) {
        User u = getUserByUsername(user.getUsername());
        if(u != null){
            return 0;
        }
        return userDao.save(user);
    }

    public int update(User user) {
        User u = getUserByUsername(user.getUsername());
        if(u !=null){//用户名发生重复
            return 0;
        }
        return userDao.update(user);
    }

    public User getUserByUsername(String username) {
        return userDao.getUserByUsername(username);
    }

    public int reset(long id) {
        User user = getUserById(id);
        if(user != null){
            user.setPassword("123456");
            return update(user);
        }
        return 0;
    }



    public Set<Authorization> getAuthorizationsById(long id) {
        return null;
    }

    public Set<Authorization> getAuthorizationsByUsername(String username) {
        return null;
    }

    public Set<String> getAuthorizationNamesById(long id) {
        return null;
    }

    public Set<String> getAuthorizationNamesByUsername(String username) {
        return null;
    }



}
